<template>
  <div class="app-container">
        <!-- 顶部功能 -->
        <div class="filter-container" style="margin-bottom: 15px">
          <!-- 一些按钮 -->
          <el-button icon="el-icon-back" @click="$emit('close-event')">返 回</el-button>
          <el-button
            class="filter-item"
            style="margin-left: 10px"
            type="primary"
            icon="el-icon-edit"
            @click="handleAdd"
          >添 加</el-button>
          <el-button
            class="filter-item"
            style="margin-left: 10px"
            type="success"
            icon="el-icon-check"
            @click="submitForm"
          >保 存</el-button>
        </div>

        <!-- 表格 -->
        <el-table ref="formRef" :data="tableData" border>
          <el-table-column type="index" label="序号" width="55" align="center" />
          <el-table-column prop="name" label="章节名" align="center">
            <template slot-scope="scope">
              <el-form :model="scope.row"  ref="formRef">
                <el-form-item prop="name" label="章节名" >
                  <el-input v-model="scope.row.name" />
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="questionCount" label="题目数量" width="100" align="center" />
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-button type="success" size="small" icon="el-icon-top" circle @click="handleUp(scope.$index, scope.row)" />
              <el-button type="primary" size="small" icon="el-icon-bottom" circle @click="handleDown(scope.$index, scope.row)" />
              <el-button type="danger" size="small" icon="el-icon-delete" circle @click="handleRemove(scope.$index, scope.row)" />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </template>

<script>
import Vue from 'vue'
import {listChapter, addChapter, updateChapter} from '@/api/teacher/chapter'

export default {
  name: 'Chapter',
  props: {
    bankId: {
      type: Number,
      required: true,
      default: 1
    }
  },
  data() {
    return {
      // 表格数据
      tableData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        tId: null,
        updataTime: null
      },
      // 显示搜索条件
      showSearch: true,
      // 加载状态
      loading: false
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询章节列表 */
    getList() {
      this.loading = true;
      listChapter(this.queryParams).then(response => {
        this.tableData = response.rows || [];
        this.loading = false;
      });
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: null,
        tId: null,
        updataTime: null
      };
      this.getList();
    },


    // 点击添加
    handleAdd() {
      const row = {
        id: null,
        bankId: this.bankId,
        name: null,
        questionCount: 0
      };
      this.tableData.push(row);
    },
// 表单重置
    reset() {
      this.form = {
        id: null,
        bId: null,
        name: null,
        radioNum: null,
        checkNum: null,
        judgmentNum: null,
        blankNum: null,
        order: null,
        shortAnswerNum: null,
        remark: null
      };
      this.resetQuery("form");
    },
    // 点击移除
    handleRemove(index, row) {
      if (row.questionCount !== 0) {
        this.$confirm('参照该章节的题目的章节属性将被置空，确认要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableData.splice(index, 1);
        });
      } else {
        this.tableData.splice(index, 1);
      }
    },

    // 上移记录
    handleUp(index, row) {
      if (index === 0) {
        this.$message.warning('已经是第一行了，不能上移');
        return;
      }
      const temp = this.tableData[index - 1];
      Vue.set(this.tableData, index - 1, this.tableData[index]);
      Vue.set(this.tableData, index, temp);
      this.$message.success('上移成功');
    },

    // 下移记录
    handleDown(index, row) {
      if (index === (this.tableData.length - 1)) {
        this.$message.warning('已经是最后一行了，不能下移');
        return;
      }
      const temp = this.tableData[index + 1];
      Vue.set(this.tableData, index + 1, this.tableData[index]);
      Vue.set(this.tableData, index, temp);
      this.$message.success('下移成功');
    },

    /** 新增按钮操作 */
    handleSave() {
      this.reset();
      this.open = true;
      this.title = "添加章节";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChapter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChapter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }

  },

};
</script>

<style scoped>
</style>
